package com.controller.ssjf;

import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.common.ajax.AjaxRes;
import com.common.mybatis.Page;
import com.common.utils.EncryptUtil;
import com.common.utils.base.Const;
import com.controller.base.BaseController;
import com.entity.Login;
import com.entity.News;
import com.entity.User;
import com.service.LoginService;
import com.service.NewsService;
import com.service.UserService;

@Controller
@RequestMapping("/ssjf/user")
public class SSJFUserController extends BaseController<Object>{
	
	@Autowired
	private LoginService loginService;
	@Autowired
	private UserService userService;
	@Autowired
	private NewsService newsService;
	
	/**
	 * 登录
	 * @param login
	 * @return
	 */
	@RequestMapping("/login")
	@ResponseBody
	public AjaxRes login(Login login){
		AjaxRes res = new AjaxRes();
		if(login.getPassword() == null || login.getPassword().equals("") 
				|| login.getAccount() == null || login.getAccount().equals("")){
			res.setFailMsg(Const.DATA_FAIL);
			return res;
		}
		String oldPass = login.getPassword();
		login.setPassword(EncryptUtil.encoderBySHA(oldPass));
		if(loginService.countPwd(login) >= 1){
			User user = new User();
			login = loginService.find(login).get(0);
			user.setLoginId(login.getId());
			try{
				user = userService.findUser(user).get(0);
				HttpSession session = getRequest().getSession();
				session.setAttribute("loggedUser", user);
				session.setAttribute("loggedType", user.getUserTypeId());
				res.setSucceedMsg(Const.DATA_SUCCEED);
			}catch(Exception e){
				e.printStackTrace();
				res.setFailMsg(Const.DATA_FAIL);
			}
		}else{
			res.setFailMsg("用户不存在或密码错误");
		}
		return res;
	}
	
	/**
	 * 注册
	 * @param login
	 * @param u
	 * @return
	 */
	@RequestMapping("/reg.dat")
	@ResponseBody
	public AjaxRes reg(Login login,User u){
		AjaxRes res = new AjaxRes();
		if(u.getUserTypeId() == null || login.getPassword() == null || login.getPassword().equals("") 
				|| login.getAccount() == null || login.getAccount().equals("")
				|| u.getTelephone() == null || u.getTelephone().equals("")){
			res.setFailMsg("注册失败");
			return res;
		}
		login.setPassword(EncryptUtil.encoderBySHA(login.getPassword()));
		loginService.reg(login,u);
		res.setSucceedMsg(Const.SAVE_SUCCEED);
		return res;
	}
	
	/**
	 * 注册状态
	 * @param model
	 * @param val
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/allstate.html")
	public String allstate(Model model,Integer val)throws Exception{
		//与自己同一类型的企业
//		String userTypeId = (String) getRequest().getSession().getAttribute("loggedType");
//		if(userTypeId == null){
//			model.addAttribute("regMsg", Const.DATA_FAIL);
//			return "ssjf/pages/login";
//		}
		String userTypeId;
		if(val != null && val == 2){
			userTypeId = "000002";
		}else{
			userTypeId = "000001";
		}
		User user = new User();
		user.setUserTypeId(userTypeId);
		Page<User> page = new Page<>();
		page.setPageSize(10);
		Page<User> users = userService.findDetailByPage(user,page);
		model.addAttribute("users", users);
		//最新新闻
		List<News> newses = newsService.findNews(5);
		model.addAttribute("newses", newses);
		return "ssjf/pages/allstate";
	}
	
	/**
	 * 我的自己的信息
	 * @return
	 */
	@RequestMapping("/me")
	@ResponseBody
	public AjaxRes me(){
		AjaxRes res = new AjaxRes();
		User user = (User) getRequest().getSession().getAttribute("loggedUser");
		user.setDetail(true);
		user.setFileRemark("portrait");
		user = userService.find(user).get(0);
		res.setSucceed(user, Const.DATA_SUCCEED);
		return res;
	}
	
	/**
	 * 我的的公共信息
	 * @param user
	 * @return
	 */
	@RequestMapping("/me.dat")
	@ResponseBody
	public AjaxRes medat(User user){
		AjaxRes res = new AjaxRes();
		user.setDetail(true);
		user = userService.find(user).get(0);
		res.setSucceed(user, Const.DATA_SUCCEED);
		return res;
	}
	
	
	@RequestMapping("/list.html")
	public String list(Model model,String type,User u, Page<User> page)throws Exception{
		Page<User> users = userService.findDetailByPage(u, page);
		model.addAttribute("users", users);
		model.addAttribute("url", "type="+type+"&userTypeId="+u.getUserTypeId()+"&pageSize="+page.getPageSize());
		model.addAttribute("type", type);
		return "ssjf/pages/list";
	}
}
